package com.cyl.XmlDemo.web.bysj;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

import com.cyl.XmlDemo.entity.Room;
import com.cyl.XmlDemo.entity.Shout;
import com.cyl.XmlDemo.entity.StompClient;

//@Controller
public class InnerBrowserMsgController {
	
	private static final Logger logger = LoggerFactory.getLogger(InnerBrowserMsgController.class);
	
//	private static List<String> clientList = new ArrayList<String>();
	
	@Autowired
	private StompClient stompClient;
	
	@Autowired
	private MsgService msgService;
	
	@Autowired
	private Room room;

	//消息中转
	@MessageMapping("/message")
	@SendTo("/topic/common/message")
	public Shout transfer(Shout shout){
		logger.debug("接收到信息："+shout.getMessage());
		return shout;
	}
	
	//定向 消息中转
	@MessageMapping("/test/message")
	public void transferV2(Shout shout,Principal principal){
//		logger.debug("v2 接收到信息："+shout.getMessage()+principal.getName());
		msgService.relayICE(principal.getName(), shout.getMessage());
	}
	
	@MessageMapping("/create-or-join")
	public void enterRoom(Principal principal){
		/*int num = stompClient.size();
		logger.debug("当前用户链接人数："+num);
		if(num == 1){
			logger.debug("创建房间");
			msgService.reactCreateOrJoin(principal.getName(), "create");
		}else if(num >= 2 && num <= 20){
			logger.debug("加入房间");
			msgService.reactCreateOrJoin(principal.getName(), "join");
		}else{
			logger.debug("房间已满");
			msgService.reactCreateOrJoin(principal.getName(), "full");
		}*/
		int num = room.size();
		logger.debug("当前频道人数： "+num);
		if(num == 1){
			logger.debug("当前频道人数只有1人，无法通话");
			msgService.reactCreateOrJoin(principal.getName(), "wait");
		}else if(num >= 2 && num <= 5){
			logger.debug("通话人数最低限制已满足，可以通话");
			msgService.reactCreateOrJoin(principal.getName());
		}else{
			logger.debug("房间已满");
			msgService.reactCreateOrJoin(principal.getName(), "full");
		}
	}
}
